package chapter15;

import java.sql.Connection;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Description:
 *
 * @author lzy
 * @create 2018-03-27 上午9:12
 */
public class EmpServiceImpl implements IEmpService {

    private ConnectionFactory dbc = new ConnectionFactory();

    @Override
    public boolean save(Emp vo) {
        Connection conn = dbc.getConn();
        IDao<Emp> dao = DaoFactory.getEmpDaoInstance(conn);
        boolean result = dao.insert(vo);
        dbc.closeConn(conn);
        return result;
    }

    @Override
    public List<Emp> getAllEmp() {
        Connection conn = dbc.getConn();
        IDao<Emp> dao = DaoFactory.getEmpDaoInstance(conn);
        List<Emp> result = dao.selectAll();
        dbc.closeConn(conn);
        return result;
    }

    @Override
    public Emp getEmpById(int id) {
        Connection conn = dbc.getConn();
        IDao<Emp> dao = DaoFactory.getEmpDaoInstance(conn);
        String condition = "empno=?";
        Emp result = dao.selectOne(condition, id);
        dbc.closeConn(conn);
        return result;
    }

    @Override
    public boolean updateEmp(Emp vo) {
        Connection conn = dbc.getConn();
        IDao<Emp> dao = DaoFactory.getEmpDaoInstance(conn);
        String condition = "empno=?";
        boolean result = false;
        result = dao.update(vo, condition, vo.getEmpno());
        dbc.closeConn(conn);
        return result;
    }

    @Override
    public boolean delEmp(int id) {
        Connection conn = dbc.getConn();
        IDao<Emp> dao = DaoFactory.getEmpDaoInstance(conn);
        String condition = "empno=?";
        boolean result = false;
        result = dao.del(condition, id);
        dbc.closeConn(conn);
        return result;
    }
}
